//answer.js
const utils = require("../../utils/util.js");
const WxParse = require("../../wxParse/wxParse.js")
var app = getApp()
Page({
  data: {
    replyMsg: '',
    title: '',
    content: '',
    createTime: '',
    replyList: [],
    articleid: ''
  },
  getSaid(e) {
    // console.log(e.detail.value)
    this.setData({
      replyMsg: e.detail.value
    })
  },
  onLoad: function (options) {
    console.log('onLoad')
    var articleId = options.articleId;
    var that = this
    //调用应用实例的方法获取全局数据
    this.refresh(articleId);
  },
  tapName: function (event) {
    console.log(event)
  },
  reply: function () {
    var that = this
    console.log(this.data.replyMsg)
    console.log(app.userid)
    console.log(this.data.articleid)
    utils.getData('replyMessage',
      {
        userId: app.userid,
        replyContent: this.data.replyMsg,
        articleId: this.data.articleid
      }, "POST").then(res => {
        if (res.data.success) {
          console.log(this.data.articleid)
          that.setData({
            replyMsg: '',
          })
          wx.showToast({
            title: "感谢回复",
            icon: '',
            duration: 2000
          });
          this.refresh(this.data.articleid);
        }else{
          wx.showToast({
            title: "感谢回复",
            icon: '',
            duration: 2000
          });
        }
      })
  },
  refresh: function (e) {
    var that = this
    //调用应用实例的方法获取全局数据
    console.log("refresh")
    console.log(e)
    utils.getData('getArticleList', { articleId: e }).then(res => {
      var article = res.data.article.articleContent;
      // TODO 
      WxParse.wxParse('article', 'html', article, that, 5);
      
      if (res.data.success) {
        that.setData({
          title: res.data.article.articleTitle,
          content: res.data.article.articleContent,
          replyList: res.data.article.reply,
          articleid: res.data.article.articleId
        });
      } else {
        wx.showToast({
          title: "程序内部错误，请稍后再试",
          icon: '',
          duration: 2000
        })
      }
    })
  }
})
